
package kaizone.haining.chaobohui.widget;

import java.util.List;

import kaizone.haining.chaobohui.R;
import kaizone.haining.chaobohui.fragment.FirstReadNearbyFragment;
import kaizone.haining.chaobohui.json.AppRecommend;
import kaizone.haining.chaobohui.json.Intro;
import kaizone.haining.chaobohui.json.Item;
import kaizone.haining.chaobohui.json.WeatherDO.WeatherInfo;
import kaizone.haining.chaobohui.util.ImageFetcher;
import android.content.Context;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextSwitcher;
import android.widget.TextView;
import android.widget.LinearLayout.LayoutParams;

public class CBHItemDropView extends LinearLayout implements OnClickListener {

    private ImageView mFlagImageView;

    private TextView mFlagTextView;

    private ImageView mExtendFlagImageView;

    private LinearLayout mTitlePanel;

    private LinearLayout mContentPanel;

    private FrameLayout mContent;

    private FragmentManager mFragmentManager;

    private OnDropClickListener mDropClickListener;

    public CBHItemDropView(Context context) {
        super(context);
        init();
    }

    public CBHItemDropView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    private void init() {

    }

    public void setFragmentManager(FragmentManager manager) {
        mFragmentManager = manager;
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        mTitlePanel = (LinearLayout) findViewById(R.id.title_panel);
        mFlagImageView = (ImageView) findViewById(R.id.img);
        mFlagTextView = (TextView) findViewById(R.id.tv01);
        mExtendFlagImageView = (ImageView) findViewById(R.id.img_drop);
        mContentPanel = (LinearLayout) findViewById(R.id.extendlayer);
        mContent = (FrameLayout) findViewById(R.id.drop_content);
        initWidget();
    }

    private void initWidget() {
        mTitlePanel.setClickable(true);
        mTitlePanel.setOnClickListener(this);
        mContentPanel.setVisibility(View.GONE);
    }

    public void setFlagImage(int resId) {
        mFlagImageView.setImageResource(resId);
    }

    public void setFlagText(String text) {
        mFlagTextView.setText(text);
    }

    public void setCultureList(List<Item> cultureList) {
        // FirstReadNearbyView firstreadnearby = (FirstReadNearbyView)
        // View.inflate(getContext(),
        // R.layout.firstread_nearby, null);
        FirstReadNearbyView firstreadnearby = new FirstReadNearbyView(getContext());
        firstreadnearby.setCultureList(cultureList);
        mContent.addView(firstreadnearby);
    }

    public void setAppRecommends(List<AppRecommend> applist) {
        FirstReadNearbyView firstreadnearby = new FirstReadNearbyView(getContext());
        firstreadnearby.loadAppRecommends(applist);
        mContent.addView(firstreadnearby);
    }

    public void setHnIntro(String intro) {
        TextView textView = new TextView(getContext());
        textView.setText(intro);
        mContent.addView(textView);
    }

    public void setchaoExpoIntro(String hnIntro, List<Item.PicModel> picmodels,
            ImageFetcher imageFetcher) {
        LinearLayout panel = (LinearLayout) View.inflate(getContext(), R.layout.image_text, null);
        // ImageView imageview = (ImageView) panel.findViewById(R.id.img01);
        TextView textView = (TextView) panel.findViewById(R.id.tv01);
        textView.setText(hnIntro);

        if (picmodels != null && !picmodels.isEmpty()) {
            int size = picmodels.size();
            for (int i = 0; i < size; i++) {
                ImageView imageview = new ImageView(getContext());
                LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                        LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
                imageview.setLayoutParams(params);
                String urlString = picmodels.get(i).url;
                imageFetcher.setImageSize(180, 180);
                imageFetcher.loadImage(urlString, imageview);
                panel.addView(imageview, i);
            }

        }

        mContent.addView(panel);
    }

    public void setWeather(WeatherInfo weatherInfo) {
        WeatherView weatherView = new WeatherView(getContext());
        weatherView.setWeatherInfo(weatherInfo);
        mContent.addView(weatherView);
    }

    public void setContentText(CharSequence text) {
        TextView textView = new TextView(getContext());
        textView.setText(text);
        mContent.addView(textView);
    }

    public void hideDropContent() {
        mExtendFlagImageView.setImageResource(R.drawable.icon_arrow_right);
        if (mContentPanel.isShown()) {
            mContentPanel.setVisibility(View.GONE);
        }
    }

    public void setDropClickListener(OnDropClickListener listener) {
        mDropClickListener = listener;
    }

    public void switchContent(Fragment fragment) {
        FragmentTransaction transaction = mFragmentManager.beginTransaction();
        transaction.replace(R.id.drop_content, fragment).commit();
    }

    public View getTitlePanel() {
        return mTitlePanel;
    }

    @Override
    public void onClick(View v) {
        if (mDropClickListener != null) {
            mDropClickListener.onStartOnClick(v);
        }
        if (v == mTitlePanel) {
            if (mContentPanel.isShown()) {
                if (mDropClickListener != null) {
                    mDropClickListener.onBeforeDropContentHide(v);
                }
                mExtendFlagImageView.setImageResource(R.drawable.icon_arrow_right);
                mContentPanel.setVisibility(View.GONE);
                if (mDropClickListener != null) {
                    mDropClickListener.onAfterDropContentHide(v);
                }
            } else {
                if (mDropClickListener != null) {
                    mDropClickListener.onBeforeDropContentExtend(v);
                }
                mExtendFlagImageView.setImageResource(R.drawable.icon_arrow_down);
                mContentPanel.setVisibility(View.VISIBLE);
                if (mDropClickListener != null) {
                    mDropClickListener.onAfterDropContentHide(v);
                }
            }
        }
    }

    public interface OnDropClickListener {

        void onStartOnClick(View v);

        void onBeforeDropContentExtend(View v);

        void onAfterDropContentExtend(View v);

        void onBeforeDropContentHide(View v);

        void onAfterDropContentHide(View v);
    }

}
